package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


 

public class DoSend extends HttpServlet {

	/**
	 * Constructor of the String.
	 */
	public DoSend() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		HttpSession se=request.getSession();
		String  host =(String)se.getAttribute("host");
		String fromMail=(String)se.getAttribute("username");//发件人用户名
		String password=(String )se.getAttribute("password");//发件人面
		
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		System.out.println("username："+fromMail);
	    System.out.println("s:"+request.getParameter("subject"));
	    System.out.println("to:"+request.getParameter("to"));
		String subject=null;
		subject=request.getParameter("subject");
 	    System.out.println("subject="+subject);
		String toMail=request.getParameter("to");
	 	System.out.println("servlet中to="+toMail);
 		String context=null;
 		context=request.getParameter("body");
   	    System.out.println("body="+context);
		
		System.out.println("username="+fromMail+" password="+password );
		String attch1_name=request.getParameter("attch1");
		System.out.println("attch1="+attch1_name);
		
 		 try {  
		        Properties props = new Properties();  
		   //   props.put("mail.smtp.starttls.enable","true");      // 设置使用SSL  
		        props.put("mail.smtp.host", host);  
		        props.put("mail.smtp.auth", "true");  
		        Session session = Session.getInstance(props);  
		        session.setDebug(true);  
		        MimeMessage message = new MimeMessage(session);  
		          
		        // 给消息对象设置收件人、发件人、主题、发信时间  
		        InternetAddress fromAddress = new  InternetAddress(fromMail);  
		        message.setFrom(fromAddress);  
		        InternetAddress toAddress = new  InternetAddress(toMail);  
		        message.setRecipient(Message.RecipientType.TO, toAddress);  
		        message.setSubject(subject);  
		        message.setContent(context,"text/html;charset=gbk");
		        message.setSentDate(new Date());  				
				message.saveChanges();  
		        Transport transport = session.getTransport("smtp");  
		        // 以smtp方式登陆邮箱，第1个参数是发送邮件用的邮件服务器SMTP地址，第2个参数为用户名，第3个参数为密码  
		        transport.connect(host, fromMail, password);  
		        transport.sendMessage(message, message.getAllRecipients());  
		        transport.close();  
		        request.setAttribute("host", host);
		        request.setAttribute("username", fromMail);
		        request.setAttribute("password", password);
		        response.sendRedirect("/Mail/send_result.jsp");
		      //  request.getRequestDispatcher("/send_result.jsp").forward(request, response);

		    } catch (MessagingException e) {  
		        e.printStackTrace();  
		    }  
 		 
 		 
 
		 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
